package com.jctrip.hotel.source.xxl.cnbe.thread;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.domain.entity.HtHotelStdInfo;
import com.jctrip.hotel.source.model.CnBooking.IdentityInfoDTO;
import com.jctrip.hotel.source.service.CNBooking.CNBookingTaskService;
import com.jctrip.hotel.source.service.basic.HtHotelStdInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author：Haruhi
 * @description:
 * @Date：2023/9/5 18:43
 * @Package：com.jctrip.hotel.source.xxl.cnbe.thread
 * @Project：hotel-main
 */
@Slf4j
public class SyncCNBEXgAmRatesTaskThread implements Runnable{
    private final IdentityInfoDTO infoDTO;

    public SyncCNBEXgAmRatesTaskThread(IdentityInfoDTO infoDTO) {
        this.infoDTO = infoDTO;
    }

    @Override
    public void run() {
        RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
        CNBookingTaskService cnBookingTaskService =
                BeanContext.getApplicationContext().getBean(CNBookingTaskService.class);
        HtHotelStdInfoService htHotelStdInfoService =
                BeanContext.getApplicationContext().getBean(HtHotelStdInfoService.class);

        while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_CNBE_XG_AM_RATES_LIST) > 0) {
            try {
                String hotelId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_CNBE_XG_AM_RATES_LIST);
                if (StringUtils.isBlank(hotelId)) {
                    continue;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();
                String start = simpleDateFormat.format(date);
                String end = simpleDateFormat.format(DateUtils.addDays(date, infoDTO.getDays()));
                HtHotelStdInfo htHotelStdInfo = htHotelStdInfoService.getOne(
                        new LambdaQueryWrapper<HtHotelStdInfo>().eq(HtHotelStdInfo::getHotelId, hotelId));
                String currency = "0001".equals(htHotelStdInfo.getCountry()) ? "CNY" : "USD";
                cnBookingTaskService.syncCNBEXgAmRatePlaneTask(htHotelStdInfo.getCountry(), currency,
                        htHotelStdInfo.getHotelId(), start, end, infoDTO);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}
